﻿module Mapparola.Client {

    export class DiscussionView extends View {

        private discussion: KnockoutObservable<Core.Client.DiscussionC>;

        private msgOpenerContainer: JQuery;

        private msgEditorIsOpened: KnockoutObservable<boolean>;
        private newMsgText: KnockoutObservable<string>;
        private isPinned: KnockoutObservable<boolean>;

        constructor(app: Mapparola.Client.App) {

            var el = $("#discussion-view").get(0);
            super(el, app);

            this.msgOpenerContainer = $(".message-opener-container", this.viewEl);

            this.discussion = ko.observable<Core.Client.DiscussionC>();
            this.newMsgText = ko.observable<string>();
            this.isPinned = ko.observable<boolean>(true);
            this.msgEditorIsOpened = ko.observable<boolean>(true);

            PubSub.subscribe("ShowDiscussion", (msg: string, discussion: Core.Client.DiscussionC) => {

                this.LoadDiscussion(discussion);

            });

            this._app.ActivateView(this.viewEl);
            ko.applyBindings(this, this.viewEl);            
        }

        public LoadDiscussion(discussion: Core.Client.DiscussionC): void {

            this.discussion(discussion);

        }

        private ToggleMsgEditorVisibility(): void {

            this.msgEditorIsOpened(!this.msgEditorIsOpened());

        }

        private SendNewMessage(): void {

            var text = this.newMsgText();

            //Create an obj of type discussionC to update the UI
            var newDiscussionC = new Core.Client.DiscussionC();
            newDiscussionC.SetText(text);
            newDiscussionC.CreatedDate = moment().utc().toDate();
            newDiscussionC.Location = this._app.Mapping.GetCurrentPoint();
            newDiscussionC.ParentToken = this.discussion().Token;

            //Create an obj of type discussion for the server
            var newDiscussion = new Core.Discussion();
            newDiscussion.SetText(text);
            newDiscussion.CreatedDate = moment().utc().toDate();
            newDiscussion.Location = this._app.Mapping.GetCurrentPoint();
            newDiscussion.ParentToken = this.discussion().Token;

            //Add at the beginning of the array
            this.discussion().OResponses.push(newDiscussionC);

            //Close editor if option is not pinned
            if (!this.isPinned()) {
                this.msgEditorIsOpened(false);
            }

            //Reset editor
            this.newMsgText("");

            //Make server request
            var args: Core.Client.AjaxRequestArgs = {
                data: newDiscussion,
                success: () => {
                    console.log("new message sent with success");
                },
                error: () => {
                    console.log("new message send failed");
                },
            };

            this._app.RestDataStore.PostNewDiscussion(newDiscussion, args);
        }

    }
}